package com.test;

import java.util.HashMap;

/**
 * Created by Administrator on 2020/9/20.
 */
public class Baidu {

    /**
     * 3
     * @param num
     * @return
     */
    public int numMax(int[] num){
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < num.length; i++){
            if (map.containsKey(i)){
                map.put(i, map.get(i) + 1);
            }else {
                map.put(i, 1);
            }
        }
        int maxSum = 0;
        int result = 0;
        for (Integer key: map.keySet()){
            if (maxSum < map.get(key)){
                maxSum = map.get(key);
                result = key;
            }
        }
        return result;
    }

    /**
     * 2、给定一个整数，求其二进制中表示中1的个数
     * @return
     */
    public int sumStr(int num){
        //将数字转化为二进制
        StringBuffer str = new StringBuffer();
        while(true){
            int result = num % 2;
            num = num / 2;
            if (num == 0){
                str.append(result);
                break;
            }
            str.append(result);
        }
        int size = 0;
        String strResult = str.toString();
        for (int i = 0; i < strResult.length(); i++){
            if ('1' == strResult.charAt(i)){
                size++;
            }
        }
        return size;
    }
}
